package com.fagertveit.android.game.cb;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import com.fagertveit.android.game.framework.Game;
import com.fagertveit.android.game.framework.Input.TouchEvent;
import com.fagertveit.android.game.framework.geom.AABB;
import com.fagertveit.android.game.framework.geom.Collision;
import com.fagertveit.android.game.framework.geom.Rectangle;
import com.fagertveit.android.game.framework.geom.Vector2;
import com.fagertveit.android.game.framework.gl.Camera2D;
import com.fagertveit.android.game.framework.gl.SpriteBatcher;
import com.fagertveit.android.game.framework.impl.GLScreen;

public class GameScreen extends GLScreen {
	Camera2D guiCam;
	SpriteBatcher batcher;
	Vector2 touchPoint;
	Pad pad;
	Ball ball;
	float frame;
	float lastX;
	float newX;
	float lengthX;
	AABB[] walls = new AABB[3];
	Level level;
	RectangularBrick brick;
	
	float nextFPSupdate;
	int fps;
	int fpsCount;
	
	public GameScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 640, 480);
		batcher = new SpriteBatcher(glGraphics, 1000);
		touchPoint = new Vector2();
		
		pad = new Pad();
		ball = new Ball();
		frame = 0;
		
		nextFPSupdate = 0.0f;
		fps = 0;
		fpsCount = 0;
		
		walls[0] = new AABB(8.0f, 240.0f, 8.0f, 240.0f);
		walls[1] = new AABB(234.0f, 8.0f, 238.0f, 8.0f);
		walls[2] = new AABB(460.0f, 240.0f, 8.0f, 240.0f);
		
		brick = new RectangularBrick(1, 1, 100, 18, 32, 35, 17);
		
		level = new Level();
	}
	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if(i == 0) {
				if(event.type == TouchEvent.TOUCH_DRAGGED) {
					lastX = newX;
					newX = event.x;
					lengthX = lastX - newX;
				} else if(event.type == TouchEvent.TOUCH_DOWN) {
					newX = event.x;
				} else {
					lengthX = 0;
				}
			}
		}
		
		nextFPSupdate += deltaTime;
		fpsCount++;
		if(nextFPSupdate > 1.0f) {
			fps = fpsCount;
			nextFPSupdate = 0.0f;
			fpsCount = 0;
		}
		
		if(ball.update(deltaTime)) {
			ball = GameCollision.wallCollide(ball, walls);
			ball = GameCollision.padCollide(pad, ball);
			ball = GameCollision.brickCollide(ball, level);
		}
		pad.update(-lengthX);
	}

	@Override
	public void render(float deltaTime) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.setViewportAndMatrices();
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		batcher.beginBatch(Assets.gameGfx);
		batcher.drawSprite(0, 0, 640, 480, Assets.gameBack);
		batcher.endBatch();
		
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.gameGfx);
		
		batcher.drawSprite(pad.getDrawX(), 448, 64, 15, Assets.bat);
		batcher.drawSprite(ball.getDrawPos().x, ball.getPos().y, 16, 16, Assets.ball);
		
		batcher.endBatch();
		
		level.render(batcher);
		
		batcher.beginBatch(Assets.fontMap);
		Assets.font.drawText(batcher, "FPS: " + fps , 16, 16);
		batcher.endBatch();
		
		gl.glDisable(GL10.GL_BLEND);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
}
